import{T as D,bq as b,B as _,m as M,s as I,C as B,br as E,d as S,k as x,a9 as V,I as U,b as f,o as y,e as l,t as p,f as L,w as O,g as $,u as q,F as C,r as F,a as A,q as H,A as j,aa as G}from"./DrFvzlh8agutd.js";import{S as K,p as z,K as g,L as J}from"./MpnPZlTSagutd.js";import{m as v,g as m}from"./CZ0h8Jpj3Xa3MT.js";import{a as P}from"./sf48gIWk3aD96J.js";import{_ as Q}from"./BfQegiIr3PfrX1.js";function h(t){const n=t.toString(16);return n.length===1?`0${n}`:n}function W(t,n,o){return`#${h(t)}${h(n)}${h(o)}`}async function X(t,n,o){const[u,e]=D(),s=new Image;s.src=`data:image/png;base64,${t.replace(/\s/m,"")}`,s.onload=e,await u;const{width:a,height:i}=s,c=document.createElement("canvas");c.width=a,c.height=i;const r=c.getContext("2d");r?.drawImage(s,0,0);const d=r?.getImageData(0,0,a,i).data;if(d){const w=a*o+n<<2,T=Math.floor(d[w]),N=Math.floor(d[w+1]),R=Math.floor(d[w+2]);return W(T,N,R)}}function Y(t){return m("account","refMM")[t]}async function Z(){const t=await b("r2mm:///accounts");if(t)return v.parse(t).map(n=>{if(n.group!==!0&&n.portfolio!==!0)return n}).filter(K)}async function tt(){return k("account","r2mm:///accounts",async t=>t.group===!0||t.portfolio===!0||_?.mmAccounts?.[t.uuid]!==!0?void 0:{_id:g(),_type:"account",refMM:{[t.uuid]:!0},title:t.name,iban:t.accountNumber,currency:t.currency,owner:t.owner,amountAccountOpening:+t.balance[0][0]})}let nt=1;async function et(){const t=new Map;return k("category","r2mm:///categories",async n=>{if(n.default===!0)return;const o=g();t.set(n.indentation,o);const u=await X(n.icon,16,16);return{_id:o,_type:"category",refMM:{[n.uuid]:!0},title:n.name,color:u,sort:++nt,parent:n.indentation>0?t.get(n.indentation-1):void 0}})}async function ot(t){const n=await b(`r2mm:///transactions?iban=${t}`);if(n){const o=m("contact","refMM"),e=v.parse(n).transactions;for(const s of e){const a=`${s.bankCode},${s.accountNumber}`;if(!o[a]){const i=g();o[a]=i,I.workspace?.createItem({_id:i,_type:"contact",refMM:{[a]:!0},title:s.name})}}}}async function at(t){const n=Y(t)??{};if(!n)return;const o=m("category","refMM"),u=m("contact","refMM");return k("pay",`r2mm:///transactions?iban=${t}`,async e=>{const s=o[e.categoryUuid],a=u[`${e.bankCode},${e.accountNumber}`],i=e.amount>0,c=Math.abs(e.amount),r=e.currency??"EUR";return{_id:g(),_type:"pay",refMM:{[e.id]:!0},title:e.purpose??"",date:E(new Date(e.bookingDate)),currency:r,credit:i,amount:B(c,r)??0,category:s,contact:a,account:n,confirmed:e.booked===!0}},e=>e.transactions)}let ct=1;async function k(t,n,o,u=e=>e){const e=await b(n);if(e){const s=v.parse(e),a=m(t,"refMM"),i=u(s),c=[];for(const r of i){if(a[r.uuid]||a[r.id])continue;const d=await o(r);d&&c.push(d),++ct%250===0&&await M(50)}for(const r of c)I.workspace?.createItem(r)}}async function gt(){P("Sync with MoneyMoney",async t=>{const n=Object.keys(_?.mmAccounts??{});if(n.length){t.setMax(2*n.length+2),await tt(),t.incResolved(),await M(100),await et(),t.incResolved(),await M(100);for(const o of n)await ot(o),t.incResolved(),await M(100),await at(o),t.incResolved();_.mmLastSync=z()}})}const st={key:0},rt={key:1},ut={key:2},it=["for"],dt=["tooltip"],ft=["id","onUpdate:modelValue"],wt=S({__name:"app-moneymoney-accounts",props:{config:{}},setup(t){const n=t,o=x(!1),u=x([]),e=V(()=>n.config.mmAccounts);async function s(){const a=await Z();o.value=a==null,u.value=G([...J(a??[])],"name")}return U(s),(a,i)=>(y(),f(C,null,[o.value?(y(),f("p",st,p(a.$t("settings.moneymoney.syncError")),1)):l("",!0),o.value?(y(),f("div",rt,[L(q(Q),{onClick:s},{default:O(()=>[$(p(a.$t("settings.moneymoney.syncRetry")),1)]),_:1})])):l("",!0),o.value?l("",!0):(y(),f("p",ut,p(a.$t("settings.moneymoney.syncAccountDesc")),1)),u.value.length?(y(!0),f(C,{key:3},F(u.value,c=>(y(),f("label",{key:c.uuid,for:c.uuid},[A("span",{tooltip:`IBAN: ${c.accountNumber}
${c.owner}`},[H(A("input",{id:c.uuid,"onUpdate:modelValue":r=>e.value[c.uuid]=r,type:"checkbox"},null,8,ft),[[j,e.value[c.uuid]]]),$(" "+p(c.name),1)],8,dt)],8,it))),128)):l("",!0)],64))}});export{wt as _,gt as s};